package com.example.mycontact

import android.app.Activity
import android.app.ComponentCaller
import android.os.Bundle
import android.view.View
import android.widget.Toast
import android.content.Intent
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.net.Uri
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.example.mycontact.DB.MyDBHelper
import com.example.mycontact.bean.Contact
import com.example.mycontact.databinding.ActivityAddContactBinding
import java.io.ByteArrayOutputStream

class AddContactActivity : AppCompatActivity(), View.OnClickListener {
    lateinit var binding: ActivityAddContactBinding
    val newContact = Contact()
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityAddContactBinding.inflate(layoutInflater)
        setContentView(binding.root)
        binding.ibSubmit.setOnClickListener(this)
        binding.ibExit.setOnClickListener(this)
        binding.ibTouxiang.setOnClickListener(this)
    }

    override fun onClick(v: View?) {
        //匹配按钮
        when(v?.id){
            R.id.ib_submit -> {
                //提交
                val name = binding.etName.text.toString().trim()
                val company = binding.etCompany.text.toString().trim()
                val position = binding.etPosition.text.toString().trim()
                val tel = binding.etNumber.text.toString().trim()
                val mail = binding.etMail.text.toString().trim()
                newContact.name = name
                newContact.tel = tel
                newContact.company = company
                newContact.position = position
                newContact.email = mail
                //将输入的数据存放到数据库
                //1.获取数据库管理对象
                val myDBHelper = MyDBHelper(this)
                //2.打开数据库.以写的方式打开数据库
                val mydb = myDBHelper.writableDatabase
                //3.写入数据
                val insertSql = "insert into Contacts(name,company,position,tel,email) values(${newContact.name},${newContact.company},${newContact.position},${newContact.tel},${newContact.email})"
                mydb.execSQL(insertSql)
                Toast.makeText(this,"添加成功",Toast.LENGTH_SHORT).show()


            }
            R.id.ib_exit -> {
                //退出
                finish()
            }
            R.id.ib_touxiang -> {
                //选择头像
                val intent = Intent(Intent.ACTION_OPEN_DOCUMENT)
                intent.addCategory(Intent.CATEGORY_OPENABLE)
                //指定显示图片
                intent.type = "image/*"
                startActivityForResult( intent,2)

            }
        }
    }

    //从写方法处理选择图片后回传的参数
    override fun onActivityResult(
        requestCode: Int,
        resultCode: Int,
        data: Intent?,
        caller: ComponentCaller
    ) {
        when(requestCode){
            2 -> {
                    if(resultCode == Activity.RESULT_OK && data!=null){
                        //把返回的uri格式转换为图片后显示到控件上
                        data.data?.let {
                            val bitmap = getBitmapFromUri(it) // 调用方法把uri转换为bitmap
                            binding.ibTouxiang.setImageBitmap(bitmap)
                            //把图片写入到数据表中
                            //把图片转换为二进制数据
                            val os: ByteArrayOutputStream = ByteArrayOutputStream()
                            bitmap?.compress(Bitmap.CompressFormat.PNG,100,os)
                            newContact.image = os.toByteArray()
                        }
                }
            }
        }
        true
    }
    fun getBitmapFromUri(uri:Uri)=contentResolver.openFileDescriptor(uri,"r")?.use {
        BitmapFactory.decodeFileDescriptor(it.fileDescriptor)
    }
}